package com.selenium;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.selenium.service.OpService;
import com.selenium.service.OpServiceImp;
import com.worktool.RestExecutor;
import com.worktool.RestTemplateConfig;
import lombok.SneakyThrows;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class Start {

    public static WebDriver driver;

    public RestTemplate restTemplate = RestExecutor.build();

    public static Set<Cookie> cookie;

//    static {
//        BufferedReader reader;
//        try {
//            reader = new BufferedReader(new FileReader(
//                    System.getProperty("user.dir") + "/"+ "config.txt"));
//            String line = reader.readLine();
//            System.getProperties().setProperty("webdriver.chrome.driver", line);
//            driver = new ChromeDriver(Config.op);
//            reader.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        System.getProperties().setProperty("webdriver.chrome.driver", Config.driverPath);
//        driver = new ChromeDriver(Config.op);
//    }

    public void startDriver(){
        System.getProperties().setProperty("webdriver.chrome.driver", Config.driverPath);
        driver = new ChromeDriver(Config.op);
    }

    public void task2(){

        OpService op = new OpServiceImp(CookieUtils.simpleCookieList(cookie));

        /**
         * 保存插件码 corpid
         */
        try {
            op.getQrCode(driver);
        } catch (Exception e) {
            System.out.println("保存插件码失败");
        }
        System.out.println("over");
    }

    public void run(){

        this.startDriver();

        driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx?from=myhome_baidu");
        driver.manage().window().maximize();

        boolean slogin = true;

        while (slogin){
            slogin = !this.sLogin();
            this.waitTime(1000L);
            System.err.println("继续查找");
        }

        System.err.println("登入成功");

        // 登录后，等待页面加载或执行其他操作
        // 这里假设Token存储在一个名为"token"的Cookie中
//        for(Cookie cookie:driver.manage().getCookies()){
//            System.err.println(cookie.getName());
//            System.err.println(cookie.getValue());
//        }
        cookie = driver.manage().getCookies();

    }



    public static int RandomNum(){
        return (int) (Math.random() * 2 + 1);
    }
    public void task(){
        OpService op = new OpServiceImp(CookieUtils.simpleCookieList(cookie));

        try {

            /**
             * 保存插件码 corpid
             */
            try {
                op.getQrCode(driver);
            } catch (Exception e) {
                System.out.println("保存插件码失败");
            }

            try {

                /**
                 * 协作-关闭邮件
                 */
                op.setBizClosed("协作-关闭邮件",driver,1);
                Thread.sleep(RandomNum() * 1000L);

                /**
                 * 协作-关闭文档
                 */
                op.mngcorpsetting("协作-关闭文档",driver,1);
                //Thread.sleep(RandomNum() * 1000L);


                /**
                 * 协作-关闭微盘
                 */
                op.updatecorpinfo("协作-关闭微盘",driver,1);
                //Thread.sleep(RandomNum() * 1000L);

            }catch (Exception e){
                System.err.println("----------关闭邮件，文档，微盘失败，请手动关闭");
                System.err.println("----------关闭邮件，文档，微盘失败，请手动关闭");
            }



            /**
             * 协作-关闭日程
             */
            op.ModifyCorpApplication("10084","协作-关闭日程");
            Thread.sleep(RandomNum() * 1000L);


            /**
             * 协作-关闭会议
             */
            op.ModifyCorpApplication("10085","协作-关闭会议");
            Thread.sleep(RandomNum() * 1000L);



            /**
             * 关闭应用管理
             */
            List<String> strings = Arrays.asList("10137", "10151","10175","10124","10129","10130","10011","10040","10041","10116","10001","10185","10034","10094","10120","10110","10063","10119","10123","10122","10097","10007");
            strings.forEach(a->{
                try {
                    Thread.sleep(RandomNum() * 1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                op.wxkf(a,"应用管理"+a);
            });


            /**
             * 关闭上下游居民群
             */
            op.closeDownstream("关闭上下游居民群");
            Thread.sleep(RandomNum() * 1000L);
            /**
             * 关闭政民朋友圈
             */
            op.closeMomentsRange("关闭政民朋友圈");
            Thread.sleep(RandomNum() * 1000L);

            /**
             * 设置上下游加入
             * 我的企业-申请加入设置
             */
            op.closeUpstream("我的企业-申请加入设置");
            Thread.sleep(RandomNum() * 1000L);
            /**
             * 管理工具-关闭成员
             */
            op.closeMember("管理工具-关闭成员");
            Thread.sleep(RandomNum() * 1000L);
            /**
             * 管理工具-关闭自动提醒群成员
             */

            /**
             * 管理工具-关闭一周小结
             */
            op.closeSummaryStep1("管理工具-关闭一周小结1");
            Thread.sleep(RandomNum() * 1000L);
            op.closeSummaryStep2("管理工具-关闭一周小结2");
            Thread.sleep(RandomNum() * 1000L);
            op.closeSummaryStep3("管理工具-关闭一周小结3");
            Thread.sleep(RandomNum() * 1000L);
            op.closeSummaryStep4("管理工具-关闭一周小结4");
            Thread.sleep(RandomNum() * 1000L);

            /**
             * 管理工具-关闭红包
             */
            op.closeRedPacket("管理工具-关闭红包");
            Thread.sleep(RandomNum() * 1000L);

            /**
             * 我的企业-关闭聊天
             */
            op.room("我的企业-关闭聊天");
            Thread.sleep(RandomNum() * 1000L);

            /**
             * 我的企业-关闭工作台
             */
            op.settings("我的企业-关闭工作台");
            Thread.sleep(RandomNum() * 1000L);
            /**
             * 我的企业-关闭微信插件
             * 在微信插件底部可快捷打开企业微信客户端
             * 成员使用微信插件时需要使用企业微信客户端
             */
            op.wxpluginSetShowBanner(2,"关闭微信插件1");
            Thread.sleep(RandomNum() * 1000L);
            op.setWxPluginInfo("关闭微信插件2");
            Thread.sleep(RandomNum() * 1000L);
            /**
             * 申请加入设置
             * 在设置上下游加入中调用
             */

            /**
             * 关闭设置
             */
            op.setting("关闭设置");


            System.out.println("over");

        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }



    public static void main(String[] args) {
        Start start = new Start();
        while (true){
            //拿取token
            start.run();
            start.task();
            driver.quit();
        }
    }

//    public static void main(String[] args) {
//        Start start = new Start();
//        while (true){
//            //拿取token
//            start.run();
//            start.task2();
//        }
//    }


    private boolean sLogin(){
        try {
            return driver.findElement(By.className("index_explore_title")).isDisplayed();
        }catch (Exception e){
            return false;
        }

    }

    /**
     * 强制等待
     * */
    private void waitTime(Long se)  {
        try {
            Thread.sleep(se);
        } catch (InterruptedException e) {
            System.err.println("等待异常");
            return;
        }
    }

    /**
     * 去 我的企业
     * */
    public void goMy(){
        CookieUtils.wait(driver,By.xpath("//*[@id=\"menu_profile\"]/span")).click();
        //this.waitTime(1000L);
    }

    /**
     * 去 微信插件
     * */
    public void goWxcj(){
        CookieUtils.wait(driver,By.xpath("//*[@id=\"profile_navigation\"]/ul/li[6]/a")).click();
        //this.waitTime(1000L);
    }

    private boolean toast(String msg){
        //修改成功提示
        int i = 0;
        while (i<5){
            if(CookieUtils.wait(driver,By.id("js_tips")).isDisplayed()){
                System.err.println(msg);
                return true;
            }
            i ++;
        }
        System.err.println(msg);
        return false;
    }


    /**
     * 在微信插件底部可快捷打开企业微信客户端
     * */
    public void sele1Wxcj(){
        WebElement ele = CookieUtils.wait(driver,By.xpath("//*[@id=\"main\"]/div/div[2]/div/div[2]/div/section[2]/div[2]/div[5]/div[2]/div[1]/label/input"));
        boolean isSelect = ele.isSelected();
        if(isSelect){
            ele.click();
            this.toast("关闭 在微信插件底部可快捷打开企业微信客户端 成功");
        }
        System.err.println("在微信插件底部可快捷打开企业微信客户端 已经关闭");
    }




}
